﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Common.Buffer
{
    /// <summary>
    /// 冒险岛封包Byte读取
    /// </summary>
    public class MaplePacketRead
    {
        MemoryStream stream = null;
        BinaryReader reader = null;

        public MaplePacketRead()
        {

        }

        public MaplePacketRead(byte[] Buff)
        {
            try
            {
                stream = new MemoryStream(Buff);
                reader = new BinaryReader(stream);
            }
            catch (Exception e)
            {
                int a = 0;
                //NGUIDebug.Log("封包初始化失败!!" + e.Message);
            }
        }


        public int ReadByteAsInt()
        {
            return reader.ReadByte();
        }

        public byte ReadByte()
        {
            return reader.ReadByte();
        }

        public int ReadInt()
        {
            int byte1 = this.reader.ReadByte();
            int byte2 = this.reader.ReadByte();
            int byte3 = this.reader.ReadByte();
            int byte4 = this.reader.ReadByte();
            return (byte4 << 24) + (byte3 << 16) + (byte2 << 8) + byte1;
        }

        public short ReadShort()
        {
            int _short1 = reader.ReadByte();
            int _short2 = reader.ReadByte();
            return (short)((_short2 << 8) + _short1);
        }


        public int ReadUShort()
        {
            int quest = ReadShort();
            if (quest < 0)
            {
                quest += 65536;
            }
            return quest;
        }

        public char ReadChar()
        {
            return (char)ReadShort();
        }

        public long ReadLong()
        {
            long byte1 = this.reader.ReadByte();
            long byte2 = this.reader.ReadByte();
            long byte3 = this.reader.ReadByte();
            long byte4 = this.reader.ReadByte();
            long byte5 = this.reader.ReadByte();
            long byte6 = this.reader.ReadByte();
            long byte7 = this.reader.ReadByte();
            long byte8 = this.reader.ReadByte();
            return (byte8 << 56) + (byte7 << 48) + (byte6 << 40) + (byte5 << 32) + (byte4 << 24) + (byte3 << 16) + (byte2 << 8) + byte1;
        }

        //public float ReadFloat()
        //{
        //    return float.Parse(ReadInt());//Float.intBitsToFloat(readInt());
        //}

        public String ReadAsciiString(int n)
        {
            byte[] ret = new byte[n];
            for (int x = 0; x < n; x++)
            {
                ret[x] = ReadByte();
            }
            try
            {
                return ASCIIEncoding.Default.GetString(ret);//new String(ret,);
            }
            catch (Exception e)
            {
                //System.err.println(e);
            }
            return "";
        }

        public String ReadMapleAsciiString()
        {
            return ReadAsciiString(ReadShort());
        }

        public byte[] Read(int num)
        {
            byte[] ret = new byte[num];
            for (int x = 0; x < num; x++)
            {
                ret[x] = ReadByte();
            }
            return ret;
        }

        public void Skip(int num)
        {
            for (int x = 0; x < num; x++)
            {
                ReadByte();
            }
        }

        public override string ToString()
        {
            long Pos = reader.BaseStream.Position;
            byte[] Bytes = stream.ToArray();
            reader.BaseStream.Position = Pos;

            return BytesToHexStr(Bytes);
        }

        string BytesToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2")+" ";
                }
            }
            return returnStr;
        }

        public byte[] ToArry()
        {
            return reader.ReadBytes((int)reader.BaseStream.Length);
        }

    }
}
